package core.data;

import java.util.HashMap;
import java.util.Map;

public class DataError {
    public static final String DEFAULT = "default";
    private final Map<String, String> messages = new HashMap<String, String>();

    @Override
    public String toString() {
        return this.getMessage();
    }

    public String getMessage(){
        return this.getMessage(null);
    }

    public String getMessage(String key){
        String message = "";
        if(key != null){
            if(this.messages.containsKey(key)){
                message = this.messages.get(key);
            }
        }else{
            key = DataError.DEFAULT;
            if(this.messages.containsKey(key)){
                message = this.messages.get(key);
            }else if(!this.messages.isEmpty()){
                key = this.messages.keySet().iterator().next();
                message = this.messages.get(key);
            }

        }
        return message;
    }

    public Map<String, String> getMessages() {
        return this.messages;
    }

    public DataError setMessage(String message){
        return this.setMessage(DataError.DEFAULT, message);
    }

    public DataError setMessage(String key, String message) {
        return this.setMessage(new HashMap<>(){{
            put(key, message);
        }});
    }
    public DataError setMessage(Map<String, String> messages){
        for (Map.Entry<String, String> entry : messages.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            this.messages.put(key, value);
        }
        return this;
    }

}
